package com.bluesky.plum.uimodels.standard.components.containers.window;

import java.util.ArrayList;
import java.util.List;

import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponent;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;

/**
 * Window is a window, it has its own Z index, its title. <br>
 * window could be a dialog, or MDI child/parent.
 * 
 * @author HZ00260
 * 
 */
public class Window extends UIComponent {

	/**
	 * Window Tree are another tree, not same with the component tree; Windows
	 * display in its own Z axis.<br>
	 * in the HTML render, Z index is useful since it can not display Real modal
	 * dialog. It only display the most font window.
	 */
	protected Window parentWindow;
	protected List<Window> childWindows;

	protected String title;

	/**
	 * A window has only one child component, that is the contentPanel. any
	 * other component should be added as the contentPanel's child.
	 */
	protected Panel contentPanel;

	// a reference to the window manager
	protected WindowManager windowManager;

	public Window() {
		super();

		visible = false;

		childWindows = new ArrayList<Window>();
	}

	protected void init() {
		contentPanel = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);

		addChildComponent(contentPanel);
	}

	@Override
	public void setUiComponentBuilder(UIComponentBuilder uiComponentBuilder) {
		// TODO Auto-generated method stub
		super.setUiComponentBuilder(uiComponentBuilder);

		init();
	}

	public Window getParentWindow() {
		return parentWindow;
	}

	public void setParentWindow(Window parentWindow) {
		// remove old parent
		if (this.parentWindow != null)
			this.parentWindow.removeChildWindow(this);

		this.parentWindow = parentWindow;

		// add new child
		if (parentWindow != null)
			parentWindow.addChildWindow(this);
	}

	public void addChildWindow(Window child) {

		child.parentWindow = this;
		childWindows.add(child);

		// register to WindowManager
		windowManager.register(child);
		child.setWindowManager(windowManager);
	}

	public void removeChildWindow(Window child) {
		child.parentWindow = null;
		childWindows.remove(child);

		// unregister from WindowManager
		windowManager.unregister(child);
		
		
		System.out.println("now font window is "+windowManager.getVisibleFontWindow().title);
	}

	public List<Window> getChildWindows() {
		return childWindows;

	}

	public Window getRootWindow() {
		if (parentWindow == null)
			return this;
		else
			return parentWindow.getRootWindow();

	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Panel getContentPanel() {
		return contentPanel;
	}

	public void setContentPanel(Panel contentPanel) {
		this.contentPanel = contentPanel;

		removeAllChildren();
		addChildComponent(contentPanel);
	}

	public void pack() {

	}

	public void repaint() {

	}

	public Thread getEventHandlerThread() {
		// so far, not useful for the HTML window classes
		throw new RuntimeException("intend to be overrided at subclass");

	}

	public WindowManager getWindowManager() {
		return windowManager;
	}

	public void setWindowManager(WindowManager windowManager) {
		this.windowManager = windowManager;
	}
}
